<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/3/28
 * Time: 13:30
 */

namespace front\modules\web\controllers;

use front\controllers\WebBaseController;
use goods\services\ShopService;
use order\services\OrderUserService;
use Overtrue\Wechat\Payment\Business;
use users\services\RegionService;
use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;

class AddressController extends WebBaseController
{

    /**
     *
     */
    public function actionIndex()
    {
        $param = [
            'getByWeb' => 1,
            'user_id' => $this->getUserId()
        ];
        $result = OrderUserService::search($param);
        $addressArr = [];
        if($result['code'] == 0){
            $addressList = $result['result'];
            $addressList = ArrayHelper::toArray($addressList);
            foreach($addressList as $key => $address){
                $add = [];
                $add['id'] = $address['id'];
                $add['name'] = $address['name'];
                $add['mobile'] = $address['mobile'];
                $add['isdefault'] = $address['default'];
                $district = RegionService::getAddress($address['district']);
                $add['detail'] = $district . $address['address'];
                array_push($addressArr, $add);
            }
        }
        return $this->render('index', ['data' => $addressArr]);
    }

    /**
     *
     */
    public function actionCreate()
    {
        if (Yii::$app->request->isAjax) {
            $params = \yii::$app->request->get();
            if($this->getUserId() == 0){
                return $this->redirect('/web/user/login');
            }
            $params['user_id'] = $this->getUserId();
            $district = $params['district'];
            $arr = explode('|',$district);
            $arr = explode(',',$arr[2]);
            $params['district'] = 0 + $arr[0];
            $params['default'] = 0 + $params['default'];
            $params['address'] = trim($params['address']);
            $result = OrderUserService::addAddress($params);
            if($result['code'] == 0){
                return 1;
            }
            return $result['message'];
        }
        return $this->render('create');
    }

    public function actionUpdate()
    {
        if (Yii::$app->request->isAjax) {
            $param = Yii::$app->request->get();
            $params['user_id'] = $this->getUserId();
            $param['getByWeb'] = 1;
            $district = $param['district'];
            $arr = explode('|',$district);
            $arr = explode(',',$arr[2]);
            $param['district'] = 0 + $arr[0];
            $param['default'] = 0 + $param['default'];
            $param['address'] = trim($param['address']);
            $param['tel'] = '213';
            $info =  OrderUserService::update($param);
            if($info['code'] == 0){
                return 1;
            }
            return $info['message'];

        }else {
            $id = yii::$app->request->get('id');
            $data['id'] = $id;
            $data['getByWeb'] = 1;
            $address = OrderUserService::search($data);
            $address = ArrayHelper::toArray($address['result']);
            $address = $address[0];
            $district = $address['district'];
            $pro = substr($district, 0 , 2);
            $city = substr($district, 0 , 4);
            $merger_name = RegionService::getMesgerName($district);
            if(!empty($merger_name)){
                $nameArr = explode(',', $merger_name);
                $address['district'] = $pro . '|' . $city . '|' . $district . ',' . $nameArr[1] . '|' . $nameArr[2] . '|' . $nameArr[3];
                $address['merger'] =  $nameArr[1] . '/' . $nameArr[2] . '/' . $nameArr[3];
            }
            return $this->render('update',['data' => $address]);
        }
    }

    /**
     * 用户自提地址列表
     * @return string
     */
    public function actionSelfAddress()
    {
        $fromInfo = Yii::$app->request->get();
        $address = ShopService::getAllAddress();
        $address = ArrayHelper::toArray($address);
        return $this->render('self',[
            'list' => $address,
            'fromInfo' => $fromInfo,
        ]);
    }

}